// pages/home/pages/party4/party4.js
var r = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //分类

    list: [],
    page: 1,
    pages: 0,
    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  //加载数据列表
  datalist(pageNo) {
    let url
    if (this.data.type == 0) {
      url = '/secretaryLetter/list'
    } else if (this.data.type == 1) {
      url = '/massesLetter/list'
    } else if (this.data.type == 2) {
      url = '/massesLetter/myLetter'
    }
    this.loading = true
    wx.showLoading()
    var param = {
      userId: wx.getStorageSync('userId'),
      page: pageNo,
      limit: 20
    };
    //获取数据列表
    r.toGet(url, param, (res) => {
      this.loading = false
      wx.hideLoading()
      if (!res.retData || !res.retData.records || res.retData.records.length == 0) {
        this.setData({
          show:true
        })
        return
      } else {
        this.setData({
          show:false,
          page: pageNo, //当前的页号
          pages: res.retData.pages, //总页数
          list: this.data.list.concat(res.retData.records)
        })
      }
    })
  },

  // 详情
  detail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../party4Detail/party4Detail?id=' + id + '&type=' + this.data.type
    })
  },

  // 赞
  fabulous(e) {
    // console.log(this.data.type)
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let list = this.data.list
    // console.log(id,index)

    if (this.data.type == 2) {
      // console.log('查看点赞')
      wx.navigateTo({
        url: '../party4Spot/party4Spot?id=' + id,
      })
      return
    }

    // 点赞
    let url
    if (this.data.type == 0) {
      url = '/secretaryLetter/addLike'
    } else if (this.data.type == 1) {
      url = '/massesLetter/addLike'
    }
    r.toPost(url, {letterId:id,userId: wx.getStorageSync('userId')}, (res) => {
      if (list[index].isLike == 0) {
        list[index].isLike = 1
        list[index].likeNum = list[index].likeNum + 1
      } else if (list[index].isLike == 1) {
        list[index].isLike = 0
        list[index].likeNum = list[index].likeNum - 1
      }
      this.setData({
        list: list
      })
    })
  },

  // 评论
  comment(e) {
    let id = e.currentTarget.dataset.id
    if (this.data.type == 2) {
      wx.navigateTo({
        url: '../party4Review/party4Review?id=' + id,
      })
    }else{
      wx.navigateTo({
        url: '../party4Detail/party4Detail?id=' + id + '&type=' + this.data.type
      })
    }
  },

  // 发布
  release() {
    wx.navigateTo({
      url: '../party4Mailbox/party4Mailbox',
    })
  },

  // 切换
  tabs(e) {
    let num = e.currentTarget.dataset.num
    this.setData({
      type: num,
      page: 1,
      pages: 0,
      list: []
    })
    this.datalist(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1,
      pages: 0,
      list: []
    })
    this.datalist(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.loading && this.data.page < this.data.pages) {
      this.datalist(this.data.page + 1)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})